<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_News_Models_Mysql_Web extends Modules_News_Models_Web
{
	protected $_table = 'news_web';
	
	public function getById($id)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE web_id = "' . $this->_conn->escape($id) . '" LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = ($this->_conn->count($rs) == 0) ? null : $this->_conn->fetchAll($rs);
		return $return;
	}
	
	public function getAllWebs($status = null)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table;
		if ($status) {
			$sql .= ' WHERE status = "' . $this->_conn->escape($status) . '"';
		}
		$sql .= ' ORDER BY name ASC';
		$rs = $this->_conn->query($sql);
		$webs = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$webs[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $webs;
	}
	
	public function add($web)
	{
		$this->_conn->insert($web, $this->_conn->_tablePrefix . $this->_table);
	}
	
	public function delete($id)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE web_id = "' . $this->_conn->escape($id) . '"';
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function update($web)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . $this->_table . ' 
					SET name = "%s", screen_name = "%s", status = "%s" 
					WHERE web_id = "%s"';
		$sql = sprintf($sql, $this->_conn->escape($web['name']),
							$this->_conn->escape($web['screen_name']),
							$this->_conn->escape($web['status']),
							$this->_conn->escape($web['web_id']));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();	
	}
	
	public function find($condition = array(), $offset = null, $limit = null)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table;
		$sql .= ' ORDER BY name ASC';
		if ($offset !== false && $limit !== false) {
			$sql .= ' LIMIT ' . (int)$offset . ',' . (int)$limit;
		}
		
		$rs = $this->_conn->query($sql);
		$webs = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$webs[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $webs;
	}
	
	public function count($condition = array())
	{
		$sql = 'SELECT COUNT(*) as num_webs FROM ' . $this->_conn->_tablePrefix . $this->_table . ' AS w LIMIT 1';
		
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_webs'];
	}
}